package cn.nebula.music.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.ClassificationDAO;
import cn.nebula.music.dao.CombinationClassificationDAO;
import cn.nebula.music.dao.SingerClassificationDAO;
import cn.nebula.music.dao.SongClassificationDAO;
import cn.nebula.music.domain.Classification;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.CombinationClassification;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.SingerClassification;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.SongClassification;

@Service
public class ClassificationServiceImpl implements ClassificationService {

	@Autowired
	private ClassificationDAO classificationDAO;

	@Autowired
	private CombinationClassificationDAO combinationClassificationDAO;

	@Autowired
	private SingerClassificationDAO singerClassificationDAO;

	@Autowired
	private SongClassificationDAO songClassificationDAO;

	@Override
	public Set<Classification> findAllClassifications() throws Exception {
		return classificationDAO.findAllClassifications();
	}

	@Override
	public Set<Classification> findAllClassificationsOfBT() throws Exception {
		return classificationDAO.findAllClassificationsOfBT();
	}

	@Override
	public Classification findClassificationByName(String name) throws Exception {
		return classificationDAO.findClassificationByName(name);
	}

	@Override
	public Classification findClassificationById(Integer id) throws Exception {
		return classificationDAO.findClassificationById(id);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addClassification(Classification classification) throws Exception {
		classificationDAO.addClassification(classification);
		Set<Combination> combinations = classification.getCombinations();
		if (combinations != null) {
			for (Combination combination : combinations) {
				CombinationClassification cc = new CombinationClassification();
				cc.setClassification(classification.getId());
				cc.setCombination(combination.getId());
				combinationClassificationDAO.addCombinationClassification(cc);
			}
		}
		Set<Song> songs = classification.getSongs();
		if (songs != null) {
			for (Song song : songs) {
				SongClassification sc = new SongClassification();
				sc.setClassification(classification.getId());
				sc.setSong(song.getId());
				songClassificationDAO.addSongClassification(sc);
			}
		}
		Set<Singer> singers = classification.getSingers();
		if (singers != null) {
			for (Singer singer : singers) {
				SingerClassification sc = new SingerClassification();
				sc.setClassification(classification.getId());
				sc.setSinger(singer.getId());
				singerClassificationDAO.addSingerClassification(sc);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateClassification(Classification classification) throws Exception {
		classificationDAO.updateClassification(classification);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<Combination> combinations1 = findClassificationById(classification.getId()).getCombinations();
		Set<Combination> combinations2 = classification.getCombinations();
		if (combinations1 != null) {
			size1 = combinations1.size();
		}
		if (combinations2 != null) {
			size2 = combinations2.size();
		}
		if (size1 == size2) {
			if (!combinations1.toString().equals(combinations2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Combination combination : combinations1) {
				CombinationClassification cc = new CombinationClassification();
				cc.setClassification(classification.getId());
				cc.setCombination(combination.getId());
				combinationClassificationDAO.deleteCombinationClassification(cc);
			}
			for (Combination combination : combinations2) {
				CombinationClassification cc = new CombinationClassification();
				cc.setClassification(classification.getId());
				cc.setCombination(combination.getId());
				combinationClassificationDAO.addCombinationClassification(cc);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Song> songs1 = findClassificationById(classification.getId()).getSongs();
		Set<Song> songs2 = classification.getSongs();
		if (songs1 != null) {
			size1 = songs1.size();
		}
		if (songs2 != null) {
			size2 = songs2.size();
		}
		if (size1 == size2) {
			if (!songs1.toString().equals(songs2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Song song : songs1) {
				SongClassification sc = new SongClassification();
				sc.setClassification(classification.getId());
				sc.setSong(song.getId());
				songClassificationDAO.deleteSongClassification(sc);
			}
			for (Song song : songs2) {
				SongClassification sc = new SongClassification();
				sc.setClassification(classification.getId());
				sc.setSong(song.getId());
				songClassificationDAO.addSongClassification(sc);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Singer> singers1 = findClassificationById(classification.getId()).getSingers();
		Set<Singer> singers2 = classification.getSingers();
		if (singers1 != null) {
			size1 = singers1.size();
		}
		if (singers2 != null) {
			size2 = singers2.size();
		}
		if (size1 == size2) {
			if (!singers1.toString().equals(singers2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Singer singer : singers1) {
				SingerClassification sc = new SingerClassification();
				sc.setClassification(classification.getId());
				sc.setSinger(singer.getId());
  				singerClassificationDAO.deleteSingerClassificationBySingerId(singer.getId());
			}
			for (Singer singer : singers2) {
				SingerClassification sc = new SingerClassification();
				sc.setClassification(classification.getId());
				sc.setSinger(singer.getId());
				//singerClassificationDAO.addSingerClassification(sc);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteClassificationById(Integer id) throws Exception {
		combinationClassificationDAO.deleteCombinationClassificationByClassificationId(id);
		songClassificationDAO.deleteSongClassificationByClassificationId(id);
		singerClassificationDAO.deleteSingerClassificationByClassificationId(id);
		classificationDAO.deleteClassificationById(id);
	}

}
